844. 比较含退格的字符串
为保证权益,题目请参考 844. 比较含退格的字符串(From LeetCode).
解决方案1
Python
python
import unittest
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
ST = []
TT = []
for s in S:
if s == "#":
if len(ST) != 0:
ST.pop()
else:
ST.append(s)
for s in T:
if s == "#":
if len(TT) != 0:
TT.pop()
else:
TT.append(s)
return ST == TT
def runTest():
so = Solution()
assert so.backspaceCompare("y#fo##f", "y#f#o##f") == True
runTest()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30